package org.edu.modules.act.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.act.common.result.ActionResult;
import org.edu.modules.act.common.result.ResultBuilder;
import org.edu.modules.act.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 流程任务历史信息的控制层
 *
 * @版权所有 福州土著人信息科技有限公司
 */
@Api(tags="业务流程历史模块")
@RestController
@RequestMapping("/activiti/history")
public class HistoryController {

	@Autowired
	private HistoryService historyService;

	/**
	 * 查询流程实例历史数据分页
	 *
	 * @param current           当前页数
	 * @param pageSize              每页记录数
	 * @param processInstanceName   流程实例名称
	 * @param processDefinitionName 流程定义名称
	 * @param status                值为personal指我的发起
	 * @return
	 */
	@ApiOperation(value="我发起的流程", notes="业务流程历史模块-我发起的流程")
	@GetMapping(path = "/queryHistoricProcessInstance")
	public Result<?> queryHistoricProcessInstance(Integer current,Integer pageSize,String processInstanceName,String processDefinitionName,String status) {
		String username = null;
			System.out.println(status+"******"+pageSize+"****"+current+"*****"+processDefinitionName+"**"+processInstanceName);
		if ("personal".equals(status)) {
//			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//			username = authentication.getName();
			//获取redis里缓存的用户
			LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			username=user.getId();
		}
		Map<String, Object> data = historyService.queryHistoricProcessInstance(current, pageSize, processInstanceName, processDefinitionName, username);
		return Result.OK(data);
	}

	/**
	 * 查询节点执行历史数据分页
	 *
	 * @param current       当前页数
	 * @param pageSize          每页记录数
	 * @param processInstanceId 流程实例ID
	 * @return
	 */
	@ApiOperation(value="查询节点执行历史数据分页", notes="业务流程历史模块-查询节点执行历史数据分页")
	@GetMapping(path = "/queryHistoricActivityInstance")
	public Result<?> queryHistoricActivityInstance(Integer current,Integer pageSize,String processInstanceId) {
		LinkedHashMap<String, Object> data = historyService.queryHistoricActivityInstance(current, pageSize, processInstanceId);
		return Result.OK(data);
	}

	/**
	 * 查询流程实例和节点执行的详细信息历史数据分页
	 *
	 * @param current       当前页数
	 * @param pageSize          每页记录数
	 * @param processInstanceId 流程实例ID
	 * @return
	 */
	@ApiOperation(value="查询流程实例和节点执行的详细信息历史数据分页", notes="业务流程历史模块-查询流程实例和节点执行的详细信息历史数据分页")
	@GetMapping(path = "/queryHistoricDetail")
	public Result<?> queryHistoricDetail(Integer current,Integer pageSize,String processInstanceId) {
		LinkedHashMap<String, Object> data = historyService.queryHistoricDetail(current, pageSize, processInstanceId);
		return Result.OK(data);
	}

	/**
	 * 查询流程实例已结束的变量历史数据分页
	 *
	 * @param current       当前页数
	 * @param pageSize          每页记录数
	 * @param processInstanceId 流程实例ID
	 * @return
	 * @throws ParseException
	 */
	@ApiOperation(value="查询流程实例已结束的变量历史数据分页", notes="业务流程历史模块-查询流程实例已结束的变量历史数据分页")
	@GetMapping(path = "/queryHistoricVariableInstance")
	public Result<?> queryHistoricVariableInstance(Integer current,Integer pageSize,String processInstanceId) throws ParseException {
		Map<String, Object> data = historyService.queryHistoricVariableInstance(current, pageSize, processInstanceId);
		return Result.OK(data);
	}

	/**
	 * 查询流程任务历史数据分页
	 *
	 * @param current 当前页数
	 * @param pageSize    每页记录数
	 * @param taskName    任务名称
	 * @return
	 */
	@ApiOperation(value="查询流程任务历史数据分页", notes="业务流程历史模块-查询流程任务历史数据分页")
	@GetMapping(path = "/queryHistoricTaskInstance")
	public Result<?> queryHistoricTaskInstance(Integer current,Integer pageSize,String taskName) {
		Map<String, Object> data = historyService.queryHistoricTaskInstance(current, pageSize, taskName);
		return Result.OK(data);
	}

	/**
	 * 删除流程实例历史数据
	 *
	 * @param processInstanceId 流程实例ID
	 * @return
	 */
	@ApiOperation(value="删除流程实例历史数据", notes="业务流程历史模块-删除流程实例历史数据")
	@PostMapping(path = "/deleteHistoricProcessInstance")
	public ActionResult deleteHistoricProcessInstance(String processInstanceId) {
		historyService.deleteHistoricProcessInstance(processInstanceId);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 删除流程任务历史数据
	 *
	 * @param id 流程任务历史ID
	 * @return
	 */
	@ApiOperation(value="删除流程任务历史数据", notes="业务流程历史模块-删除流程任务历史数据")
	@PostMapping(path = "/deleteHistoricTaskInstance")
	public ActionResult deleteHistoricTaskInstance(String id) {
		historyService.deleteHistoricTaskInstance(id);
		return ResultBuilder.buildActionSuccess();
	}

}
